package com.shortlink.gateway.config;

import cn.hutool.core.text.AntPathMatcher;
import cn.hutool.core.util.ObjectUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 权限过滤器配置信息类
 *
 * @author wp
 */
@Data
@Component
@ConfigurationProperties(prefix = "shortlink-gateway.auth")
public class AuthFilterProperties {
    /**
     * 白名单请求
     */
    private List<String> whitePathList;
    /**
     * 路径匹配规则
     */
    private final AntPathMatcher antPathMatcher = new AntPathMatcher();

    public boolean isInWhite(String path) {
        if (ObjectUtil.isEmpty(whitePathList)) {
            return false;
        }

        for (String whitePath : whitePathList) {
            if (antPathMatcher.match(whitePath, path)) {
                return true;
            }
        }
        return false;
    }
}